home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Burning & Media / GB-PVR 1.2.13 / GBPVR10213.msi / Cabs.w1.cab / TreeViewAdapter.cs466 < prev    next >
Text File  |  2007-12-22  |  26KB  |  712 lines

  1. using System;
  2. using System.Collections.Specialized;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Web;
  6. using System.Web.Configuration;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12.  
  13. using System.Reflection;
  14.  
  15. namespace gbweb
  16. {
  17.     public class TreeViewAdapter : System.Web.UI.WebControls.Adapters.HierarchicalDataBoundControlAdapter, IPostBackEventHandler, IPostBackDataHandler
  18.     {
  19.         private WebControlAdapterExtender _extender = null;
  20.         private WebControlAdapterExtender Extender
  21.         {
  22.             get
  23.             {
  24.                 if (((_extender == null) && (Control != null)) ||
  25.                     ((_extender != null) && (Control != _extender.AdaptedControl)))
  26.                 {
  27.                     _extender = new WebControlAdapterExtender(Control);
  28.                 }
  29.  
  30.                 System.Diagnostics.Debug.Assert(_extender != null, "CSS Friendly adapters internal error", "Null extender instance");
  31.                 return _extender;
  32.             }
  33.         }
  34.  
  35.         private int _checkboxIndex = 1;
  36.         private HiddenField _viewState = null;
  37.         private bool _updateViewState = false;
  38.         private string _newViewState = "";
  39.  
  40.         public TreeViewAdapter()
  41.         {
  42.             if (_viewState == null)
  43.             {
  44.                 _viewState = new HiddenField();
  45.             }
  46.         }
  47.  
  48.         // Implementation of IPostBackDataHandler
  49.         public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
  50.         {
  51.             return true;
  52.         }
  53.  
  54.         public virtual void RaisePostDataChangedEvent()
  55.         {
  56.             TreeView treeView = Control as TreeView;
  57.             if (treeView != null)
  58.             {
  59.                 TreeNodeCollection items = treeView.Nodes;
  60.                 _checkboxIndex = 1;
  61.                 UpdateCheckmarks(items);
  62.             }
  63.         }
  64.  
  65.         // Implementation of IPostBackEventHandler
  66.         public void RaisePostBackEvent(string eventArgument)
  67.         {
  68.             TreeView treeView = Control as TreeView;
  69.             if (treeView != null)
  70.             {
  71.                 TreeNodeCollection items = treeView.Nodes;
  72.                 if (!String.IsNullOrEmpty(eventArgument))
  73.                 {
  74.                     if (eventArgument.StartsWith("s") || eventArgument.StartsWith("e"))
  75.                     {
  76.                         string selectedNodeValuePath = eventArgument.Substring(1).Replace("\\", "/");
  77.                         TreeNode selectedNode = treeView.FindNode(selectedNodeValuePath);
  78.                         if (selectedNode != null)
  79.                         {
  80.                             bool bSelectedNodeChanged = selectedNode != treeView.SelectedNode;
  81.                             ClearSelectedNode(items);
  82.                             selectedNode.Selected = true; // does not raise the SelectedNodeChanged event so we have to do it manually (below).
  83.                             if (eventArgument.StartsWith("e"))
  84.                             {
  85.                                 selectedNode.Expand();
  86.                             }
  87.  
  88.                             if (bSelectedNodeChanged)
  89.                             {
  90.                                 Extender.RaiseAdaptedEvent("SelectedNodeChanged", new EventArgs());
  91.                             }
  92.                         }
  93.                     }
  94.                     else if (eventArgument.StartsWith("p"))
  95.                     {
  96.                         string parentNodeValuePath = eventArgument.Substring(1).Replace("\\", "/");
  97.                         TreeNode parentNode = treeView.FindNode(parentNodeValuePath);
  98.                         if ((parentNode != null) && ((parentNode.ChildNodes == null) || (parentNode.ChildNodes.Count == 0)))
  99.                         {
  100.                             parentNode.Expand(); // Raises the TreeNodePopulate event
  101.                         }
  102.                     }
  103.                 }
  104.             }
  105.         }
  106.  
  107.         protected override Object SaveAdapterViewState()
  108.         {
  109.             string retStr = "";
  110.             TreeView treeView = Control as TreeView;
  111.             if ((treeView != null) && (_viewState != null))
  112.             {
  113.                 if ((_viewState != null) && (Page != null) && (Page.Form != null) && (!Page.Form.Controls.Contains(_viewState)))
  114.                 {
  115.                     Panel panel = new Panel();
  116.                     panel.Controls.Add(_viewState);
  117.                     Page.Form.Controls.Add(panel);
  118.                     string script = "document.getElementById('" + _viewState.ClientID + "').value = GetViewState__AspNetTreeView('" + Extender.MakeChildId("UL") + "');";
  119.                     Page.ClientScript.RegisterOnSubmitStatement(typeof(TreeViewAdapter), _viewState.ClientID, script);
  120.                 }
  121.                 retStr = _viewState.UniqueID + "|" + ComposeViewState(treeView.Nodes, "");
  122.             }
  123.             return retStr;
  124.         }
  125.  
  126.         protected override void LoadAdapterViewState(Object state)
  127.         {
  128.             TreeView treeView = Control as TreeView;
  129.             string oldViewState = state as String;
  130.             if ((treeView != null) && (oldViewState != null) && (oldViewState.Split('|').Length == 2))
  131.             {
  132.                 string hiddenInputName = oldViewState.Split('|')[0];
  133.                 string oldExpansionState = oldViewState.Split('|')[1];
  134.                 if (!treeView.ShowExpandCollapse)
  135.                 {
  136.                     _newViewState = oldExpansionState;
  137.                     _updateViewState = true;
  138.                 }
  139.                 else if (!String.IsNullOrEmpty(Page.Request.Form[hiddenInputName]))
  140.                 {
  141.                     _newViewState = Page.Request.Form[hiddenInputName];
  142.                     _updateViewState = true;
  143.                 }
  144.             }
  145.         }
  146.  
  147.         protected override void OnInit(EventArgs e)
  148.         {
  149.             if (Extender.AdapterEnabled)
  150.             {
  151.                 _updateViewState = false;
  152.                 _newViewState = "";
  153.  
  154.                 TreeView treeView = Control as TreeView;
  155.                 if (treeView != null)
  156.                 {
  157.                     treeView.EnableClientScript = false;
  158.                 }
  159.             }
  160.  
  161.             base.OnInit(e);
  162.  
  163.             if (Extender.AdapterEnabled)
  164.             {
  165.                 RegisterScripts();
  166.             }
  167.         }
  168.  
  169.         protected override void OnLoad(EventArgs e)
  170.         {
  171.             base.OnLoad(e);
  172.  
  173.             TreeView treeView = Control as TreeView;
  174.             if (Extender.AdapterEnabled && _updateViewState && (treeView != null))
  175.             {
  176.                 treeView.CollapseAll();
  177.                 ExpandToState(treeView.Nodes, _newViewState);
  178.                 _updateViewState = false;
  179.             }
  180.         }
  181.  
  182.         private void RegisterScripts()
  183.         {
  184.             Extender.RegisterScripts();
  185.             string folderPath = WebConfigurationManager.AppSettings.Get("CSSFriendly-JavaScript-Path");
  186.             if (String.IsNullOrEmpty(folderPath))
  187.             {
  188.                 folderPath = "~/JavaScript";
  189.             }
  190.             string filePath = folderPath.EndsWith("/") ? folderPath + "TreeViewAdapter.js" : folderPath + "/TreeViewAdapter.js";
  191.             Page.ClientScript.RegisterClientScriptInclude(GetType(), GetType().ToString(), Page.ResolveUrl(filePath));
  192.         }
  193.  
  194.         protected override void RenderBeginTag(HtmlTextWriter writer)
  195.         {
  196.             if (Extender.AdapterEnabled)
  197.             {
  198.                 Extender.RenderBeginTag(writer, "AspNet-TreeView");
  199.             }
  200.             else
  201.             {
  202.                 base.RenderBeginTag(writer);
  203.             }
  204.         }
  205.  
  206.         protected override void RenderEndTag(HtmlTextWriter writer)
  207.         {
  208.             if (Extender.AdapterEnabled)
  209.             {
  210.                 Extender.RenderEndTag(writer);
  211.             }
  212.             else
  213.             {
  214.                 base.RenderEndTag(writer);
  215.             }
  216.         }
  217.  
  218.         protected override void RenderContents(HtmlTextWriter writer)
  219.         {
  220.             if (Extender.AdapterEnabled)
  221.             {
  222.                 TreeView treeView = Control as TreeView;
  223.                 if (treeView != null)
  224.                 {
  225.                     writer.Indent++;
  226.                     _checkboxIndex = 1;
  227.                     BuildItems(treeView.Nodes, true, true, writer);
  228.                     writer.Indent--;
  229.                     writer.WriteLine();
  230.                 }
  231.             }
  232.             else
  233.             {
  234.                 base.RenderContents(writer);
  235.             }
  236.         }
  237.  
  238.         private void BuildItems(TreeNodeCollection items, bool isRoot, bool isExpanded, HtmlTextWriter writer)
  239.         {
  240.             if (items.Count > 0)
  241.             {
  242.                 writer.WriteLine();
  243.  
  244.                 writer.WriteBeginTag("ul");
  245.  
  246.                 if (isRoot)
  247.                 {
  248.                     writer.WriteAttribute("id", Extender.MakeChildId("UL"));
  249.                 }
  250.                 if (!isExpanded)
  251.                 {
  252.                     writer.WriteAttribute("class", "AspNet-TreeView-Hide");
  253.                 }
  254.                 writer.Write(HtmlTextWriter.TagRightChar);
  255.                 writer.Indent++;
  256.  
  257.                 foreach (TreeNode item in items)
  258.                 {
  259.                     BuildItem(item, writer);
  260.                 }
  261.  
  262.                 writer.Indent--;
  263.                 writer.WriteLine();
  264.                 writer.WriteEndTag("ul");
  265.             }
  266.         }
  267.  
  268.         private void BuildItem(TreeNode item, HtmlTextWriter writer)
  269.         {
  270.             TreeView treeView = Control as TreeView;
  271.             if ((treeView != null) && (item != null) && (writer != null))
  272.             {
  273.                 writer.WriteLine();
  274.                 writer.WriteBeginTag("li");
  275.                 writer.WriteAttribute("class", GetNodeClass(item));
  276.                 writer.Write(HtmlTextWriter.TagRightChar);
  277.                 writer.Indent++;
  278.                 writer.WriteLine();
  279.  
  280.                 if (IsExpandable(item) && treeView.ShowExpandCollapse)
  281.                 {
  282.                     WriteNodeExpander(treeView, item, writer);
  283.                 }
  284.  
  285.                 if (IsCheckbox(treeView, item))
  286.                 {
  287.                     WriteNodeCheckbox(treeView, item, writer);
  288.                 }
  289.                 else if (IsLink(item))
  290.                 {
  291.                     WriteNodeLink(treeView, item, writer);
  292.                 }
  293.                 else
  294.                 {
  295.                     WriteNodePlain(treeView, item, writer);
  296.                 }
  297.  
  298.                 if (HasChildren(item))
  299.                 {
  300.                     BuildItems(item.ChildNodes, false, item.Expanded.Equals(true), writer);
  301.                 }
  302.  
  303.                 writer.Indent--;
  304.                 writer.WriteLine();
  305.                 writer.WriteEndTag("li");
  306.             }
  307.         }
  308.  
  309.         private void WriteNodeExpander(TreeView treeView, TreeNode item, HtmlTextWriter writer)
  310.         {
  311.             writer.WriteBeginTag("span");
  312.             writer.WriteAttribute("class", (item.Expanded.Equals(true) ? "AspNet-TreeView-Collapse" : "AspNet-TreeView-Expand"));
  313.             if (HasChildren(item))
  314.             {
  315.                 writer.WriteAttribute("onclick", "ExpandCollapse__AspNetTreeView(this)");
  316.             }
  317.             else
  318.             {
  319.                 writer.WriteAttribute("onclick", Page.ClientScript.GetPostBackEventReference(treeView, "p" + (Page.Server.HtmlEncode(item.ValuePath)).Replace("/", "\\"), true));
  320.             }
  321.             writer.Write(HtmlTextWriter.TagRightChar);
  322.             writer.Write(" ");
  323.             writer.WriteEndTag("span");
  324.             writer.WriteLine();
  325.         }
  326.  
  327.         private void WriteNodeImage(TreeView treeView, TreeNode item, HtmlTextWriter writer)
  328.         {
  329.             string imgSrc = GetImageSrc(treeView, item);
  330.             if (!String.IsNullOrEmpty(imgSrc))
  331.             {
  332.                 writer.WriteBeginTag("img");
  333.                 writer.WriteAttribute("src", treeView.ResolveClientUrl(imgSrc));
  334.                 writer.WriteAttribute("alt", !String.IsNullOrEmpty(item.ToolTip) ? item.ToolTip : (!String.IsNullOrEmpty(treeView.ToolTip) ? treeView.ToolTip : item.Text));
  335.                 writer.Write(HtmlTextWriter.SelfClosingTagEnd);
  336.             }
  337.         }
  338.  
  339.         private void WriteNodeCheckbox(TreeView treeView, TreeNode item, HtmlTextWriter writer)
  340.         {
  341.             writer.WriteBeginTag("input");
  342.             writer.WriteAttribute("type", "checkbox");
  343.             writer.WriteAttribute("id", treeView.ClientID + "n" + _checkboxIndex.ToString() + "CheckBox");
  344.             writer.WriteAttribute("name", treeView.UniqueID + "n" + _checkboxIndex.ToString() + "CheckBox");
  345.  
  346.             if (!String.IsNullOrEmpty(treeView.Attributes["OnClientClickedCheckbox"]))
  347.             {
  348.                 writer.WriteAttribute("onclick", treeView.Attributes["OnClientClickedCheckbox"]);
  349.             }
  350.  
  351.             if (item.Checked)
  352.             {
  353.                 writer.WriteAttribute("checked", "checked");
  354.             }
  355.             writer.Write(HtmlTextWriter.SelfClosingTagEnd);
  356.  
  357.             if (!String.IsNullOrEmpty(item.Text))
  358.             {
  359.                 writer.WriteLine();
  360.                 writer.WriteBeginTag("label");
  361.                 writer.WriteAttribute("for", treeView.ClientID + "n" + _checkboxIndex.ToString() + "CheckBox");
  362.                 writer.Write(HtmlTextWriter.TagRightChar);
  363.                 writer.Write(item.Text);
  364.                 writer.WriteEndTag("label");
  365.             }
  366.  
  367.             _checkboxIndex++;
  368.         }
  369.  
  370.  
  371.         private void WriteNodeLink(TreeView treeView, TreeNode item, HtmlTextWriter writer)
  372.         {
  373.             writer.WriteBeginTag("a");
  374.  
  375.             if (!String.IsNullOrEmpty(item.NavigateUrl))
  376.             {
  377.                 writer.WriteAttribute("href", Extender.ResolveUrl(item.NavigateUrl));
  378.             }
  379.             else
  380.             {
  381.                 string codePrefix = "";
  382.                 if (item.SelectAction == TreeNodeSelectAction.Select)
  383.                 {
  384.                     codePrefix = "s";
  385.                 }
  386.                 else if (item.SelectAction == TreeNodeSelectAction.SelectExpand)
  387.                 {
  388.                     codePrefix = "e";
  389.                 }
  390.                 else if (item.PopulateOnDemand)
  391.                 {
  392.                     codePrefix = "p";
  393.                 }
  394.                 writer.WriteAttribute("href", Page.ClientScript.GetPostBackClientHyperlink(treeView, codePrefix + (Page.Server.HtmlEncode(item.ValuePath)).Replace("/", "\\"), true));
  395.             }
  396.  
  397.             WebControlAdapterExtender.WriteTargetAttribute(writer, item.Target);
  398.  
  399.             if (!String.IsNullOrEmpty(item.ToolTip))
  400.             {
  401.                 writer.WriteAttribute("title", item.ToolTip);
  402.             }
  403.             else if (!String.IsNullOrEmpty(treeView.ToolTip))
  404.             {
  405.                 writer.WriteAttribute("title", treeView.ToolTip);
  406.             }
  407.             writer.Write(HtmlTextWriter.TagRightChar);
  408.             writer.Indent++;
  409.             writer.WriteLine();
  410.  
  411.             WriteNodeImage(treeView, item, writer);
  412.             writer.Write(item.Text);
  413.  
  414.             writer.Indent--;
  415.             writer.WriteEndTag("a");
  416.         }
  417.  
  418.         private void WriteNodePlain(TreeView treeView, TreeNode item, HtmlTextWriter writer)
  419.         {
  420.             writer.WriteBeginTag("span");
  421.             if (IsExpandable(item))
  422.             {
  423.                 writer.WriteAttribute("class", "AspNet-TreeView-ClickableNonLink");
  424.                 if (treeView.ShowExpandCollapse)
  425.                 {
  426.                     writer.WriteAttribute("onclick", "ExpandCollapse__AspNetTreeView(this.parentNode.getElementsByTagName('span')[0])");
  427.                 }
  428.             }
  429.             else
  430.             {
  431.                 writer.WriteAttribute("class", "AspNet-TreeView-NonLink");
  432.             }
  433.             writer.Write(HtmlTextWriter.TagRightChar);
  434.             writer.Indent++;
  435.             writer.WriteLine();
  436.  
  437.             WriteNodeImage(treeView, item, writer);
  438.             writer.Write(item.Text);
  439.  
  440.             writer.Indent--;
  441.             writer.WriteEndTag("span");
  442.         }
  443.  
  444.         private void UpdateCheckmarks(TreeNodeCollection items)
  445.         {
  446.             TreeView treeView = Control as TreeView;
  447.             if ((treeView != null) && (items != null))
  448.             {
  449.                 foreach (TreeNode item in items)
  450.                 {
  451.                     if (IsCheckbox(treeView, item))
  452.                     {
  453.                         string name = treeView.UniqueID + "n" + _checkboxIndex.ToString() + "CheckBox";
  454.                         bool bIsNowChecked = (Page.Request.Form[name] != null);
  455.                         if (item.Checked != bIsNowChecked)
  456.                         {
  457.                             item.Checked = bIsNowChecked;
  458.                             Extender.RaiseAdaptedEvent("TreeNodeCheckChanged", new TreeNodeEventArgs(item));
  459.                         }
  460.                         _checkboxIndex++;
  461.                     }
  462.  
  463.                     if (HasChildren(item))
  464.                     {
  465.                         UpdateCheckmarks(item.ChildNodes);
  466.                     }
  467.                 }
  468.             }
  469.         }
  470.  
  471.         private bool IsLink(TreeNode item)
  472.         {
  473.             return (item != null) && ((!String.IsNullOrEmpty(item.NavigateUrl)) || item.PopulateOnDemand || (item.SelectAction == TreeNodeSelectAction.Select) || (item.SelectAction == TreeNodeSelectAction.SelectExpand));
  474.         }
  475.  
  476.         private bool IsCheckbox(TreeView treeView, TreeNode item)
  477.         {
  478.             bool bItemCheckBoxDisallowed = (item.ShowCheckBox != null) && (item.ShowCheckBox.Value == false);
  479.             bool bItemCheckBoxWanted = (item.ShowCheckBox != null) && (item.ShowCheckBox.Value == true);
  480.             bool bTreeCheckBoxWanted =
  481.                  (treeView.ShowCheckBoxes == TreeNodeTypes.All) ||
  482.                  ((treeView.ShowCheckBoxes == TreeNodeTypes.Leaf) && (!IsExpandable(item))) ||
  483.                  ((treeView.ShowCheckBoxes == TreeNodeTypes.Parent) && (IsExpandable(item))) ||
  484.                  ((treeView.ShowCheckBoxes == TreeNodeTypes.Root) && (item.Depth == 0));
  485.  
  486.             return (!bItemCheckBoxDisallowed) && (bItemCheckBoxWanted || bTreeCheckBoxWanted);
  487.         }
  488.  
  489.         private string GetNodeClass(TreeNode item)
  490.         {
  491.             string value = "AspNet-TreeView-Leaf";
  492.             if (item != null)
  493.             {
  494.                 if (item.Depth == 0)
  495.                 {
  496.                     if (IsExpandable(item))
  497.                     {
  498.                         value = "AspNet-TreeView-Root";
  499.                     }
  500.                     else
  501.                     {
  502.                         value = "AspNet-TreeView-Root AspNet-TreeView-Leaf";
  503.                     }
  504.                 }
  505.                 else if (IsExpandable(item))
  506.                 {
  507.                     value = "AspNet-TreeView-Parent";
  508.                 }
  509.  
  510.                 if (item.Selected)
  511.                 {
  512.                     value += " AspNet-TreeView-Selected";
  513.                 }
  514.                 else if (IsChildNodeSelected(item))
  515.                 {
  516.                     value += " AspNet-TreeView-ChildSelected";
  517.                 }
  518.                 else if (IsParentNodeSelected(item))
  519.                 {
  520.                     value += " AspNet-TreeView-ParentSelected";
  521.                 }
  522.             }
  523.             return value;
  524.         }
  525.  
  526.         private string GetImageSrc(TreeView treeView, TreeNode item)
  527.         {
  528.             string imgSrc = "";
  529.  
  530.             if ((treeView != null) && (item != null))
  531.             {
  532.                 imgSrc = item.ImageUrl;
  533.  
  534.                 if (String.IsNullOrEmpty(imgSrc))
  535.                 {
  536.                     if (item.Depth == 0)
  537.                     {
  538.                         if ((treeView.RootNodeStyle != null) && (!String.IsNullOrEmpty(treeView.RootNodeStyle.ImageUrl)))
  539.                         {
  540.                             imgSrc = treeView.RootNodeStyle.ImageUrl;
  541.                         }
  542.                     }
  543.                     else
  544.                     {
  545.                         if (!IsExpandable(item))
  546.                         {
  547.                             if ((treeView.LeafNodeStyle != null) && (!String.IsNullOrEmpty(treeView.LeafNodeStyle.ImageUrl)))
  548.                             {
  549.                                 imgSrc = treeView.LeafNodeStyle.ImageUrl;
  550.                             }
  551.                         }
  552.                         else if ((treeView.ParentNodeStyle != null) && (!String.IsNullOrEmpty(treeView.ParentNodeStyle.ImageUrl)))
  553.                         {
  554.                             imgSrc = treeView.ParentNodeStyle.ImageUrl;
  555.                         }
  556.                     }
  557.                 }
  558.  
  559.                 if ((String.IsNullOrEmpty(imgSrc)) && (treeView.LevelStyles != null) && (treeView.LevelStyles.Count > item.Depth))
  560.                 {
  561.                     if (!String.IsNullOrEmpty(treeView.LevelStyles[item.Depth].ImageUrl))
  562.                     {
  563.                         imgSrc = treeView.LevelStyles[item.Depth].ImageUrl;
  564.                     }
  565.                 }
  566.             }
  567.  
  568.             return imgSrc;
  569.         }
  570.  
  571.         private bool HasChildren(TreeNode item)
  572.         {
  573.             return ((item != null) && ((item.ChildNodes != null) && (item.ChildNodes.Count > 0)));
  574.         }
  575.  
  576.         private bool IsExpandable(TreeNode item)
  577.         {
  578.             return (HasChildren(item) || ((item != null) && item.PopulateOnDemand));
  579.         }
  580.  
  581.         private void ClearSelectedNode(TreeNodeCollection nodes)
  582.         {
  583.             if (nodes != null)
  584.             {
  585.                 foreach (TreeNode node in nodes)
  586.                 {
  587.                     if (node.Selected)
  588.                     {
  589.                         node.Selected = false;
  590.                     }
  591.                     if (node.ChildNodes != null)
  592.                     {
  593.                         ClearSelectedNode(node.ChildNodes);
  594.                     }
  595.                 }
  596.             }
  597.         }
  598.  
  599.         private bool IsChildNodeSelected(TreeNode item)
  600.         {
  601.             bool bRet = false;
  602.  
  603.             if ((item != null) && (item.ChildNodes != null))
  604.             {
  605.                 bRet = IsChildNodeSelected(item.ChildNodes);
  606.             }
  607.  
  608.             return bRet;
  609.         }
  610.  
  611.         private bool IsChildNodeSelected(TreeNodeCollection nodes)
  612.         {
  613.             bool bRet = false;
  614.  
  615.             if (nodes != null)
  616.             {
  617.                 foreach (TreeNode node in nodes)
  618.                 {
  619.                     if (node.Selected || IsChildNodeSelected(node.ChildNodes))
  620.                     {
  621.                         bRet = true;
  622.                         break;
  623.                     }
  624.                 }
  625.             }
  626.  
  627.             return bRet;
  628.         }
  629.  
  630.         private bool IsParentNodeSelected(TreeNode item)
  631.         {
  632.             bool bRet = false;
  633.  
  634.             if ((item != null) && (item.Parent != null))
  635.             {
  636.                 if (item.Parent.Selected)
  637.                 {
  638.                     bRet = true;
  639.                 }
  640.                 else
  641.                 {
  642.                     bRet = IsParentNodeSelected(item.Parent);
  643.                 }
  644.             }
  645.  
  646.             return bRet;
  647.         }
  648.  
  649.         private string ComposeViewState(TreeNodeCollection nodes, string state)
  650.         {
  651.             if (nodes != null)
  652.             {
  653.                 foreach (TreeNode node in nodes)
  654.                 {
  655.                     if (IsExpandable(node))
  656.                     {
  657.                         if (node.Expanded.Equals(true))
  658.                         {
  659.                             state += "e";
  660.                             state = ComposeViewState(node.ChildNodes, state);
  661.                         }
  662.                         else
  663.                         {
  664.                             state += "n";
  665.                         }
  666.                     }
  667.                 }
  668.             }
  669.  
  670.             return state;
  671.         }
  672.  
  673.         private string ExpandToState(TreeNodeCollection nodes, string state)
  674.         {
  675.             if ((nodes != null) && (!String.IsNullOrEmpty(state)))
  676.             {
  677.                 foreach (TreeNode node in nodes)
  678.                 {
  679.                     //Added second part of if clause to prevent crash on empty state occuring ofter the intitial check
  680.                     if ((IsExpandable(node))&& (!String.IsNullOrEmpty(state)))
  681.                     {
  682.                         bool bExpand = (state[0] == 'e');
  683.                         state = state.Substring(1);
  684.                         if (bExpand)
  685.                         {
  686.                             node.Expand();
  687.                             state = ExpandToState(node.ChildNodes, state);
  688.                         }
  689.                     }
  690.                 }
  691.             }
  692.  
  693.             return state;
  694.         }
  695.  
  696.         static public void ExpandToDepth(TreeNodeCollection nodes, int expandDepth)
  697.         {
  698.             if (nodes != null)
  699.             {
  700.                 foreach (TreeNode node in nodes)
  701.                 {
  702.                     if (node.Depth < expandDepth)
  703.                     {
  704.                         node.Expand();
  705.                         ExpandToDepth(node.ChildNodes, expandDepth);
  706.                     }
  707.                 }
  708.             }
  709.         }
  710.     }
  711. }
  712.